package handlers

import (
	"context"
	"net/http"
	"time"

	"github.com/AiGenApps/goout-server/database"
	"github.com/gin-gonic/gin"
	"go.mongodb.org/mongo-driver/bson"
)

func TestDB(c *gin.Context) {
	collection := database.GetCollection("test", "test_collection")

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	// 插入测试文档
	doc := bson.M{
		"test_field": "test_value",
		"timestamp":  time.Now(),
	}

	_, err := collection.InsertOne(ctx, doc)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"error": "数据库操作失败",
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"message": "数据库连接测试成功",
	})
}
